package com.example.boot.controller.mail;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class MailController {

    @Resource
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String username;
    private String emailTo = "2824143998@qq.com";
    private String emailName = "--邮件名称--";

    @GetMapping("/testMail")
    public String testMail() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        simpleMailSend(emailTo, "--主题--", "--邮件内容--" + format);
        return "success";
    }

    /**
     * 无附件 简单文本内容发送
     * @param email   接收方email
     * @param subject 邮件内容主题
     * @param text    邮件内容
     */
    public void simpleMailSend(String email, String subject, String text) {
        //创建邮件内容
        SimpleMailMessage message = new SimpleMailMessage();
        //这里指的是发送者的账号
        message.setFrom(username);
        message.setTo(email);
        message.setSubject(subject);
        message.setText(text);
        //发送邮件
        mailSender.send(message);
        System.out.println("\033[32;1m" + "发送给 " + email + " 的邮件发送成功" + "\033[0m");
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       接受人
     * @param subject  主题
     * @param html     发送内容
     * @param filePath 附件路径
     */
    public void sendAttachmentMail(String to, String subject, String html, String filePath) throws MessagingException, UnsupportedEncodingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.setFrom(username, emailName);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(html, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        messageHelper.addAttachment(fileName, file);
        mailSender.send(mimeMessage);
    }

    /**
     * 发送html内容的 邮件
     *
     * @param email
     * @param subject
     * @param text
     */
    public void sendSimpleMailHtml(String email, String subject, String text) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(username);
        helper.setTo("demogogo@yeah.net");
        helper.setSubject("主题：嵌入静态资源");
        // 注意<img/>标签，src='cid:jpg'，'cid'是contentId的缩写，'jpg'是一个标记
        helper.setText("<html><body><img src=\"cid:jpg\"></body></html>", true);
        // 加载文件资源，作为附件
        FileSystemResource file = new FileSystemResource(new File("C:\\Users\\吴超\\Pictures\\Camera Roll\\微信截图_20191016142536.png"));
        // 调用MimeMessageHelper的addInline方法替代成文件('jpg[标记]', file[文件])
        helper.addInline("jpg", file);
        // 发送邮件
        mailSender.send(mimeMessage);
    }
}
